<?php

namespace System\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\Container;
use System\Model\Utility;
use System\Model\Videos;
use System\Model\Actionimg;

class VideosController extends AbstractActionController {

    private function checklogin() {
        $session = new Container("ssadmin");
        $username = $session->adminusername;
        if ($username == null) {
            $this->redirect()->toUrl(WEB_PATH . "/cpadmin");
        }
    }

    private $VideosTable;

    private function getVideosTable() {
        if (!$this->VideosTable) {
            $pst = $this->getServiceLocator();
            $this->VideosTable = $pst->get("System\Model\VideosTable");
        }
        return $this->VideosTable;
    }

    public function indexAction() {

        $this->checklogin();
        $this->layout("layout/layoutadmin");
    }

    public function loadvideosAction() {
        $objul = new Utility();
        $row_per = 50;
        //$start=1;
        $start = addslashes(trim($this->params()->fromQuery("page")));
        if ($start == null) {
            $start = addslashes(trim($this->params()->fromPost("page")));
        }
        $key = addslashes(trim($this->params()->fromPost("key")));
        $listvideos = $this->getVideosTable()->loadvideosmodel($row_per, $start, $key);
        $listcount = ceil($this->getVideosTable()->countloadvideos($key) / $row_per);
        $view = "";
        $index = 1;
        if (count($listvideos) > 0) {
            foreach ($listvideos as $key => $listdata) {
                $status = '<span class="label label-success">Đang chạy</span>';
                if ($listdata["videoActive"] == false) {
                    $status = '<span class="label label-danger">Tạm ngưng</span>';
                }
                $view .='<tr id="' . $listdata["videoId"] . '">' .
                        '<td>' . $index . '</td>' .
                        '<td>' . $listdata["videoName"] . '</td>' .
                        '<td>' . $listdata["videosView"] . '</td>' .
                        '<td>' . $objul->formatDate($listdata["videoDatePost"]) . '</td>' .
                        '<td>' . $objul->formatDate($listdata["videoDateUpdate"]) . '</td>' .
                        '<td>' . $status . '</td>' .
                        '<td>' .
                        '<a onclick="delcatalog(' . $listdata["videoId"] . ');" style="padding:0px 5px;background: #990000;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-trash-o"></i></a>' .
                        '<a style="padding:0px 5px;background: #ff9933;color:#FFF;border-radius: 3px;" href="' . WEB_PATH . '/systemadmin/videos/edit?videosId=' . $listdata["videoId"] . '" title=""><i class="fa fa-pencil-square-o"></i></a>';
                if ($listdata["videoActive"] == false) {
                    $view.='<a onclick="setActive(' . $listdata["videoId"] . ',1);" style="padding:0px 5px;background: #006600;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-unlock-alt"></i></a>';
                } else {
                    $view .='<a onclick="setActive(' . $listdata["videoId"] . ',0);" style="padding:0px 5px;background: #990000;color:#FFF;border-radius: 3px;" href="javascript:void(0);" title=""><i class="fa fa-lock"></i></a>';
                }

                $view .='</td>' .
                        '</tr>';
                $index ++;
            }
            $view .="*duypc*";
            if ($listcount > 1) {
                for ($i = 0; $i < $listcount; $i++) {
                    $view.='<a class="pageadmin" id="page' . ($i + 1) . '" onclick="setpage(' . ($i + 1) . ');" href="javascript:void(0);">' . ($i + 1) . '</a>';
                }
            }
        } else {
            $view .="Chưa có videos nào.*duypc*";
        }
        echo $view;
        die;
    }

    public function addvideosAction() {
        $this->layout("layout/layoutadmin");
        $action = $this->params()->fromPost("btn");
        if ($action != null) {
            $this->actionaddvideos();
        } else {
            $this->checklogin();
        }
    }

    public function geturlvideosAction() {
        $objul = new Utility();
        $title = $objul->removescript(addslashes(trim($this->params()->fromPost("titlevideos"))));
        $splitTitle = $objul->chuyenDoi($title);
        $checkurl = $this->getVideosTable()->checkurlvideos($splitTitle);
        echo $checkurl;
        die;
    }

    private function actionaddvideos() {
        $objUl = new Utility();
        $videoTitle = $objUl->removescript(addslashes(trim($this->params()->fromPost("titlevideos"))));
        if ($videoTitle == null || strlen($videoTitle) < 6) {
            echo "err*duypc*<br/> - Tiêu đề videos lớn hơn 6 kí tự - <i style='color:#F56954;'>Lỗi</i>";
            die;
        }
        $videolink = $this->getVideosTable()->checkurlvideos($objUl->chuyenDoi($videoTitle));
        $videosDes = $objUl->removescript(addslashes(trim($this->params()->fromPost("desvideos"))));
        $videoFrame = $this->params()->fromPost("framevideos");
        $checkcode = $objUl->checkdeCode($videoFrame);
        if ($checkcode == false) {
            echo "err*duypc*<br/> - Frame bạn nhập có thể chứa mã độc. - <i style='color:#F56954;'>Lỗi</i>";
            die;
        }
        //Action upload and crop img
        $objImg = new Actionimg();
        //url hinh anh
        $url = $this->params()->fromPost("urlimg");
        $tmpimg = "";
        $filename = "";
        $dirpath = str_replace("\\", "/", APPLICATION_PATH . "/public/imgvideo/");
        $dirpaththumb = $dirpath . "imgthumb/";
        $fileimgnew = "";
        if ($url != null) {
            $tmpimg = $url;
            $filename = $url;
        } else {
            $tmpimg = $_FILES["imgfile"]["tmp_name"];
            $filename = $_FILES["imgfile"]["name"];
        }
        $ext = substr(strrchr($filename, '.'), 1);
        $fileimgnew = time() . "." . $ext;
        copy($tmpimg, $dirpaththumb . $fileimgnew);
        $objImg->load($tmpimg);
        $objImg->resizeToHeight(400);
        $objImg->save($dirpath . $fileimgnew);
        @$objImg->croppThis($dirpaththumb . $fileimgnew);
        
        $data = array();
        $data["videoName"] = $videoTitle;
        $data["videoDescription"] = $videosDes;
        $data["videoLink"] = $videolink;
        $data["videoImg"] = "imgvideo/" . $fileimgnew;
        $data["videoImgThumb"] = "imgvideo/imgthumb/" . $fileimgnew;
        $data["videoContent"] = $videoFrame;
        $data["videoDatePost"] = Date("Y:m:d");
        $data["videoDateUpdate"] = Date("Y:m:d");
        $data["videoActive"] = true;
        $objVideos = new Videos();
        $objVideos->exchangeArray($data);
        $add = $this->getVideosTable()->addvideos($objVideos);
        if ($add) {
            echo "ok*duypc*<br/> - thêm video ok. - <i style='color:#F56954;'>OK</i>";
        } else {
            echo "err*duypc*<br/> - Máy chủ đang bận vui lòng thử lại sau. - <i style='color:#F56954;'>Lỗi</i>";
        }
        die;
    }

    public function activeAction() {
        $videosId = addslashes(trim($this->params()->fromPost("videosId")));
        $active = addslashes(trim($this->params()->fromPost("active")));
        $this->getVideosTable()->status($videosId, $active);
        die;
    }

    public function delvideosAction() {
        $videosId = addslashes(trim($this->params()->fromPost("videosId")));
        $this->getVideosTable()->delImg($videosId);
        $checkdel = $this->getVideosTable()->delvideos($videosId);
        if ($checkdel) {
            echo "delok*duypc*Xóa videos thành công.";
        } else {
            echo "delok*duypc*Đã phát sinh ra lỗi khi thực hiện lệnh xóa.";
        }
        die;
    }

    public function editAction() {
        $this->layout("layout/layoutadmin");
        $action = $this->params()->fromPost("btn");
        if ($action != null) {
            $this->actionedit();
        } else {
            $videosId = addslashes(trim($this->params()->fromQuery("videosId")));
            $dataedit = $this->getVideosTable()->getdataedit($videosId);
             return array("dataedit" => $dataedit);
        }
    }

    private function actionedit() {
        $objUl = new Utility();
        $videoTitle = $objUl->removescript(addslashes(trim($this->params()->fromPost("titlevideos"))));
        if ($videoTitle == null || strlen($videoTitle) < 6) {
            echo "err*duypc*<br/> - Tiêu đề videos lớn hơn 6 kí tự - <i style='color:#F56954;'>Lỗi</i>";
            die;
        }
        $videolink = $this->getVideosTable()->checkurlvideos($objUl->chuyenDoi($videoTitle));
        $videosId = $this->params()->fromPost("videosId");
        $videosDes = $objUl->removescript(addslashes(trim($this->params()->fromPost("desvideos"))));
        $videoFrame = $this->params()->fromPost("framevideos");
        $checkcode = $objUl->checkdeCode($videoFrame);
        $flagimg = $this->params()->fromPost("flagImg");
        $data = array();
        $data["videoName"] = $videoTitle;
        $data["videoDescription"] = $videosDes;
        $data["videoLink"] = $videolink;
        if ($checkcode == false) {
            echo "err*duypc*<br/> - Frame bạn nhập có thể chứa mã độc. - <i style='color:#F56954;'>Lỗi</i>";
            die;
        }
        if ($flagimg == "0") {

            //Action upload and crop img
            $this->getVideosTable()->delImg($videosId);
            $objImg = new Actionimg();
            //url hinh anh
            $url = $this->params()->fromPost("urlimg");
            $tmpimg = "";
            $filename = "";
            $dirpath = str_replace("\\", "/", APPLICATION_PATH . "/public/imgvideo/");
            $dirpaththumb = $dirpath . "imgthumb/";
            $fileimgnew = "";
            if ($url != null) {
                $tmpimg = $url;
                $filename = $url;
            } else {
                $tmpimg = $_FILES["imgfile"]["tmp_name"];
                $filename = $_FILES["imgfile"]["name"];
            }
            $ext = substr(strrchr($filename, '.'), 1);
            $fileimgnew = time() . "." . $ext;
            copy($tmpimg, $dirpaththumb . $fileimgnew);
            $objImg->load($tmpimg);
            $objImg->resizeToHeight(400);
            $objImg->save($dirpath . $fileimgnew);
            @$objImg->croppThis($dirpaththumb . $fileimgnew);
            $data["videoImg"] = "imgvideo/" . $fileimgnew;
            $data["videoImgThumb"] = "imgvideo/imgthumb/" . $fileimgnew;
        }
        $data["videoContent"] = $videoFrame;
        $data["videoDateUpdate"] = Date("Y:m:d");
        $add = $this->getVideosTable()->updatevideos($data, $videosId);
        if ($add) {
            echo "ok*duypc*<br/> - update video ok. - <i style='color:#F56954;'>OK</i>";
        } else {
            echo "err*duypc*<br/> - Máy chủ đang bận vui lòng thử lại sau. - <i style='color:#F56954;'>Lỗi</i>";
        }
    }

}
